'use strict'

###*
 # @ngdoc function
 # @name xoceanApp.controller:SubjectCtrl
 # @description
 # # SubjectCtrl
 # Controller of creating a new subject
###
angular.module('xoceanApp')
  .controller 'NewSubjectCtrl',['$scope', '$location', '$stateParams', 'template', 'subject', ($scope, $location, $stateParams, template, subject) ->

    $scope.selectedTmpl = null #已选择的模板
    $scope.html = '' # 已选择的模板的html内容

    #  专题基本信息
    $scope.basicInfo = {
      modelid: ''
      modelname: ''
      timeout: 0
      freq: 0
      keywords: ''
      digest: ''
      thumbnail: ''
    }

    # content字段为空的专题自动跳转
    if subject and subject.html is ''
      $location.url("/#{TOPICID}/wizard/step_1")
      
    if $stateParams.subjectId
      $scope.html = subject.html
      delete subject.html
      $scope.basicInfo = subject
      # $scope.basicInfo.modelid = subject.modelid.slice(4)
      $scope.isEditing = true
    else if $stateParams.newFromSubject
      # 从已有专题新建专题
      $scope.html = subject.html
      $scope.basicInfo.thumbnail = subject.thumbnail
    else if $stateParams.newFromTemplate
      # 从模板新建专题
      $scope.html = template.html
      $scope.basicInfo.thumbnail = template.thumbnail
      
    else if $stateParams.templateId
      # 编辑模板
      $scope.selectedTmpl = {
        thumbnail: template.thumbnail
        name: template.name
      }
      $scope.html = template.html
    
    $scope.$on '$stateChangeSuccess', ->
      $scope.step = $location.path().split('/').pop()
      return

    return
  ]

    
